package com.chuanjiabao.wisdom.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.databinding.WidgetMainTableViewBinding;

@SuppressWarnings("CStyleArrayDeclaration")
public class MainTableView extends LinearLayout {

    private OnTableChangedListener OnTableChangedListener;
    private int lastIndex = 0;
    private ImageView imgs[] = new ImageView[3];
    private int defaultImgRes[] = new int[3];
    private int selectImgRes[] = new int[3];
    private TextView tvModuleTexts[] = new TextView[3];
    private Context mcontext;

    public MainTableView(Context context) {
        this(context, null);
    }

    @SuppressLint("ClickableViewAccessibility")
    public MainTableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        WidgetMainTableViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.widget_main_table_view, this, true);
        binding.setPresenter(this);

        mcontext = context;

        imgs[0] = binding.ivModuleFirst;
        imgs[1] = binding.ivModuleQuanZi;
        imgs[2] = binding.ivModuleClass;

        tvModuleTexts[0] = binding.tvModuleFirst;
        tvModuleTexts[1] = binding.tvModuleSecond;
        tvModuleTexts[2] = binding.tvModuleThird;

        defaultImgRes[0] = R.drawable.icon_family_select_no;
        selectImgRes[0] = R.drawable.icon_family_select_yes;
        defaultImgRes[1] = R.drawable.icon_secret_select_no;
        selectImgRes[1] = R.drawable.icon_secret_select_yes;
        defaultImgRes[2] = R.drawable.icon_reasion_select_no;
        selectImgRes[2] = R.drawable.icon_reasion_select_yes;

        binding.llModuleSecret.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (OnTableChangedListener != null) return OnTableChangedListener.onTabTouched();
                else return false;
            }
            return false;
        });
    }

    public void onClick(View view) {
        int clickAt = Integer.parseInt(view.getTag() + "");
        onClick(clickAt);
    }

    /**
     * 手动触发点击事件
     */
    public void onClick(int index) {
        if (lastIndex == index) {
            return;
        }
        imgs[lastIndex].setImageResource(defaultImgRes[lastIndex]);
        imgs[index].setImageResource(selectImgRes[index]);
        for(TextView tv: tvModuleTexts){
            tv.setTextColor(mcontext.getResources().getColor(R.color.white));
        }
        tvModuleTexts[index].setTextColor(mcontext.getResources().getColor(R.color.red));
        lastIndex = index;
        if (OnTableChangedListener != null) {
            OnTableChangedListener.onTableChanged(index);
        }
    }

    public void setOnTableChangedListener(OnTableChangedListener onTableChangedListener) {
        this.OnTableChangedListener = onTableChangedListener;
    }

    public interface OnTableChangedListener {
        void onTableChanged(int index);
        boolean onTabTouched();
    }

}


